/* SWwork,
 * por victor [at] superwaba [dot] com [dot] br
 * <victor felipe loureiro lima>
 * 
 * This is an example application coded using SuperWaba
 * its meant as a learning source  for  other SW users
 * on how to use and build a  real-life solution/appli-
 * cation using SuperWaba.
 * 
 * -- Everything is well-divided:
 *    - cat, holds all record information regarding the Catalog
 *      read/write, and information of what is an Order, a Customer,
 *      or Product.
 *    - ui, as the name suggests, holds all user interfaces
 *      for all the windows you might see on this application.
 *      
 * thanks to: Guich for his programming skills, advices, and changes
 *            on this code, and Renato Ribeiro for usefull brainstormings
 *            and good advices on the user-interface 
 */
package mobile.ui;

import mobile.dominio.Parametros;
import mobile.persistencia.PParametros;
import waba.ui.*;
import waba.fx.*;
import waba.sys.*;
import waba.util.*;

public class LaAnfitriona extends MainWindow
{    
   public static Color defaultBackColor = Color.getColor(0xd9d9ff);
   public static Color defaultForeColor = Color.getColor(0x000099);
   private MenuBar mbar;
   
   /* constants for the swap() method 
    * each one assings a window to be swapped to
    */
   public static final int PRINCIPAL       = 0;
   public static final int MENUCLIENTES   = 1;
   public static final int NUEVOCLIENTE    = 2;
   public static final int BUSCARCLIENTE = 3;
   public static final int MENUVENTAS      = 4;
   public static final int NUEVAVENTA       = 5;
   public static final int BUSCARVENTA    = 6;
   public static final int MODIFPARAM    = 7;
   public static final int AUXILIAR = 8;
   public static final int NUEVOMOVIMIENTO = 9;
   public static final int BUSCARMOVIMIENTO = 10;
   public static final int MENUMOVIMIENTO = 11;
   public static final int DUMMY = -1;
   
   private static String[] classNames =
   {
         "mobile.ui.Principal",
         "mobile.ui.cliente.MenuCliente",
         "mobile.ui.cliente.NuevoCliente",
         "mobile.ui.cliente.BuscarCliente",
         "mobile.ui.venta.MenuVenta",
         "mobile.ui.venta.NuevaVenta",
         "mobile.ui.venta.BuscarVenta",
         "mobile.ui.parametro.ModificarParametros",
         "mobile.ui.Auxiliar",
         "mobile.ui.movimiento.NuevoMovimiento",
         "mobile.ui.movimiento.BuscarMovimiento",
         "mobile.ui.movimiento.MenuMovimiento",
   };
   
   /** The instantiated classes */
   private static Container[] screens; 
   
   public LaAnfitriona(){
      super("La Anfitriona mobile", TAB_ONLY_BORDER);
      Settings.setUIStyle(waba.sys.Settings.Flat);
      
      setBackColor(Color.WHITE );
      UIColors.controlsBack = Color.WHITE;
      
      Random aleatorio = new Random();
      
      int pos = aleatorio.rand(1, 1000);
      
      pos = pos > 500 ? 2 : 1;
      
      Parametros losParametros = PParametros.instance.getParametroXID(1);;
      losParametros.setPosicionGrilla(pos);
      
      pos = aleatorio.rand(1, 1000);
      
      pos = pos > 500 ? 2 : 1;
      
      losParametros.setPosicionGuardar(pos);
      
      PParametros.instance.write(losParametros, true);
      // Instantiate the screens
      screens = new Container[classNames.length];
   }
   
   public void onStart() 
   {    
      MenuItem col0[] = 
      {
         new MenuItem("Archivo"),
         new MenuItem("Acerca de"),
      };
      
      setMenuBar(mbar = new MenuBar(new MenuItem[][]{col0}));
      swapTo(PRINCIPAL);
   }   
   
   /**
    * This method swap windows around, i.e., this is how we
    * jump from one window to another.
    * 
    * @param x - A constant which assigns the window to swap to
    */
   public static void swapTo(int x)
   {
      if (x != DUMMY) 
      {
         if (screens[x] == null)
            try
            {
               screens[x] = (Container)Class.forName(classNames[x]).newInstance();
            }
            catch (Exception e)
            {
               throw new RuntimeException("Class "+classNames[x]+" cannot be instantiated!");
            }
           
         MainWindow.getMainWindow().swap(screens[x]);
      }
   }
   
   public void onEvent( Event e ) 
   {
      switch( e.type ) 
      {
       case ControlEvent.PRESSED:
         if( e.target == mbar )
            switch( mbar.getSelectedMenuItem() )
            {
               case 1: 
                  new MessageBox("Acerca de","La Anfitriona mobile|Software para el reparto de soda|Creado por Diego Rostagnol|Tel: 099504732").popupModal();
               break;
            }
      }
   }
}
